<?php
/*require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Http_Client');
Zend_Loader::loadClass('Zend_Http_Client_Adapter_Socket');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Json');
Zend_Loader::loadClass('Zend_Gdata_App_CaptchaRequiredException');

session_start();

$client = $_SESSION['client'];
*/
require_once 'events.php';

$start = split("T", $_POST['start']);
$end = split("T", $_POST['end']);
$invitees = split("\|", trim($_POST['invitees'], "\|"));
$title = $_POST['title'];
$desc = $_POST['desc'];
$calendarURI = $_POST['caluri'];
//echo "$title, $desc, '', $start[0], $start[1], $end[0], $end[1], '-07', $calendarURI, $invitees";
scheduleMeeting($client, $title, $desc, '', $start[0], $start[1], $end[0], $end[1], '-07', '', $invitees);

/**
 * scheduleMeeting: schedules a meeting as an event with any number of invited
 * 	guests.
 * @param whosgoing: a 2D array. It is an array of attendees, where each
 * 	attendee is an array of [email]. I'm working on getting Name in there
 * 	or seeing if we even need/want name.
 * @return same return as createEvent
 **/


function scheduleMeeting($client, $title, $desc='', $where='', $startDate, $startTime='', $endDate, $endTime = '', $tzOffset = '-07', $calendarURI='', $whosgoing = '') {

	if ($whosgoing==''){
		return createEvent($client, $title, $desc, $where, $startDate,
			$startTime, $endDate, $endTime, $tzOffset, $calendarURI);
	}

	$gdataCal = new Zend_Gdata_Calendar($client);
	$newEvent = $gdataCal->newEventEntry();
	$whoArray=array();
	foreach($whosgoing as $who){
		$tempWho = $gdataCal->newWho();
		$tempWho->email = $who;
		$tempWho->rel = 'http://schemas.google.com/g/2005#event.attendee';
		$whoArray[]=$tempWho;
	}

	$newEvent->who = $whoArray;	
	$newEvent->title = $gdataCal->newTitle($title);
	$newEvent->where = array($gdataCal->newWhere($where));
	$newEvent->content = $gdataCal->newContent("$desc");
	$when = $gdataCal->newWhen();

	if ($startTime!='' && $endTime!=''){
	 	$when->startTime = "{$startDate}T{$startTime}.000{$tzOffset}:00";
		$when->endTime = "{$endDate}T{$endTime}.000{$tzOffset}:00";
	}else {
		$when->startTime = $startDate;
		$when->endTime = $endDate;
	}
	$newEvent->when = array($when);
	if ($calendarURI==''){
		$createdEvent = $gdataCal->insertEvent($newEvent);
	}
	else {
		$createdEvent = $gdataCal->insertEvent($newEvent,$calendarURI);
	}
	return $createdEvent->id->text;

}
?>
